/**************************************
These regressions generate estimates in Table 4

Author: Andrew Foote
Date: 02/04/2019


**************************************/
#delimit ;
set more off; 

include "./config.do" ;
cap log close; 
log using "./03.regressions_byyearpost.log", replace; 
use "$datadir/earnings_regressions.dta", clear; 

des;

tab year_grad;  

/*************************************
First, keep only people
who aren't missing demographics.
*************************************/

include "./restrictions.do" ;

/*********************************************************
Going to run a number of regressions (controlling for 
demographics) 

Outcomes: 

Total Earnings
Total Earnings > 0
Log Earnings
p25 log
p50 log
p75 log
**********************************************************/  

local demogs "male white hispanic black asian inst_state*" ;


postfile support spec str15 comment str8 category Nobs share_flagship
                                using $supportdir/support_regs_byyearpost.dta, replace;


/*************************************************************
These next three loops are for certain years after graduation:

1, 5, 10

**************************************************************/

/* to ensure a consistent sample */
  
  keep if year_grad <= 2006 ;

foreach yrpost in 1 5 10 { ;
foreach category in national instate { ;
di "*************************************";
di "**** YEAR POST: `yrpost' **************" ;
di "**** Category: `category'    ************" ;
di "*************************************" ;
	reg `category'_earnings flagship i.year i.quarter i.year_grad `demogs' if year-year_grad == `yrpost'
                            , cluster(pik) ;

              local betayr`yrpost'_`category'_1 = _b[flagship];
              local seyr`yrpost'_`category'_1 = _se[flagship] ;
              local Nyr`yrpost'_`category'_1 = e(N) ;

              sum flagship if e(sample) ;
               post support (1) ("Year `yr'") ("`category'") (r(N)) (r(mean)) ;                         

	reg `category'_earnings flagship i.year i.quarter i.year_grad `demogs' if year-year_grad == `yrpost'
                            & `category'_earnings>0, cluster(pik) ;        

              local betayr`yrpost'_`category'_2 = _b[flagship];
              local seyr`yrpost'_`category'_2 = _se[flagship] ;
              local Nyr`yrpost'_`category'_2 = e(N) ;

              sum flagship if e(sample) ;
               post support (2) ("Year `yr'") ("`category'") (r(N)) (r(mean)) ;                         
	
        reg log_earn_`category' flagship i.year i.quarter i.year_grad `demogs' if year-year_grad== `yrpost'
                            & `category'_earnings >0, vce(cluster pik) ;

              local betayr`yrpost'_`category'_3 = _b[flagship];
              local seyr`yrpost'_`category'_3 = _se[flagship] ;
              local Nyr`yrpost'_`category'_3 = e(N) ;

               sum flagship if e(sample) ;
               post support (3) ("Year `yr'") ("`category'") (r(N)) (r(mean)) ;  
	                        
	
	foreach pctile in 25 50 75 90 { ;
		di "********************************************" ;
		di "***************** QREG: `pctile' *************" ;
		di "*********************************************";
		qreg log_earn_`category' flagship i.year i.quarter i.year_grad `demogs' 
			if year-year_grad == `yrpost'  & `category'_earnings>0,  q(`pctile');

                        local betayr`yrpost'_`category'_`pctile' = _b[flagship];
                        local seyr`yrpost'_`category'_`pctile' = _se[flagship] ;
                        local Nyr`yrpost'_`category'_`pctile' = e(N) ;

                         sum flagship if e(sample) ;
                         post support (`pctile') ("Year `yr'") ("`category'") (r(N)) (r(mean)) ;              
	} ;
	
} ;
} ;





postclose support ; 

use $supportdir/support_regs_byyearpost.dta, clear ; 

list ;

/**********************************************
This end of file puts out the coefficients and bias terms
***********************************************/


foreach spec in yr1 yr5 yr10 { ;
di "Coefficients for specification `spec'" ;
foreach ord in 1 2 3 25 50 75 90{ ;                              
        foreach coeff in beta se N { ;
                local bias = `beta`spec'_national_`ord'' - `beta`spec'_instate_`ord'' ;
                foreach category in national instate { ;
                       di %8.4f ``coeff'`spec'_`category'_`ord'' "      ", _continue ;        
                };
                if "`coeff'" == "beta" { ;
                     di %8.4f `bias', _continue; 
                };
                di "" ;
        } ;
} ; 

} ;                                     
